# -*- coding:utf-8 -*-
# @Author:🎈RedBalloon
# @Time:2022/9/30-13:02
# @File:06-post请求的使用2.py
from urllib.request import urlopen, Request
from urllib.parse import urlencode
from fake_useragent import UserAgent


# POST请求模拟登录笔趣阁网网站（这个没有上一个那么严格）
url = "https://www.qbiqu.com/login.php?do=submit&jumpurl=http%3A%2F%2Fwww.qbiqu.com%2Fuserdetail.php%3Fuid%3Dundefined"
headers = {
    "User-Agent": UserAgent().random,
}
form_data = {
    "username": "redballoon",
    "password": "123456",
    "usecookie": 0,
    "action": "login"
}
form_data = urlencode(form_data).encode("utf-8")
request = Request(url, headers=headers, data=form_data)
response = urlopen(request).read()
print(response.decode('gbk'))  # <title>登录成功</title>


# 如果报编码问题的错误，建议多了解些编码方式 ：gbk、gb2312、utf-8、acsii等
# 还有爬取前可以看一下网站请求头的  Accept-Language: 看看是编码方式，这样后续解码编码也会减少不必要的编码错误
